package com.xjgc.util;

/**
 * @author: 小逗号
 * @date: 2023-11-22
 * @description:
 * @since:
 */
public class StringUtils {
    /*在不以000开头的字符串添加000*/
    public static String addPrefix(String oldStr) {
        if (org.springframework.util.StringUtils.isEmpty(oldStr)) {
            return null;
        }
        if (!oldStr.startsWith("000")) {
            oldStr = "000" + oldStr;
        }
        return oldStr;
    }

    /**
     * 从字符串右侧开始,提取连续数值
     *
     * @param source
     * @return
     */
    public static String extractNumberRight(String source) {
        StringBuffer sb = new StringBuffer();
        char[] chars = source.toCharArray();
        for (int i = chars.length - 1; i >= 0; i--) {
            char c = chars[i];
            if (Character.isDigit(c) || '.' == c) {
                sb.insert(0, c);
            }
        }
        return sb.toString();
    }

    /**
     * 左侧开头0 去除
     * @param str
     * @return
     */
    public static String left0Trim(String str) {
        if (org.apache.commons.lang3.StringUtils.isEmpty(str)) {
            return str;
        }
        if (str.startsWith("0")) {
            str = str.substring(1);
            return left0Trim(str);
        }
        return str;
    }
}
